package cn.lds.healthj377.service.impl;

import cn.lds.healthj377.domain.AppointmentRecord;
import cn.lds.healthj377.dto.DsHealthConstant;
import cn.lds.healthj377.dto.Pager;
import cn.lds.healthj377.dto.ResponseDTO;
import cn.lds.healthj377.exception.SysException;
import cn.lds.healthj377.mapper.AppointmentRecordMapper;
import cn.lds.healthj377.service.AppointmentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
public class AppointmentRecordServiceImpl implements AppointmentRecordService {

    @Autowired
    private AppointmentRecordMapper appointmentRecordMapper;

    @Override
    public ResponseDTO<List<AppointmentRecord>> selectAppointmentRecordByCondition(AppointmentRecord appointmentRecord, Pager pager) throws SysException {

        int count = appointmentRecordMapper.count(appointmentRecord);

        pager.setTotalRow(count);


        List<AppointmentRecord> appointmentRecords = appointmentRecordMapper.selectAppointmentRecordByCondition(appointmentRecord,pager);

        if (CollectionUtils.isEmpty(appointmentRecords)){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.QUERY_FAIL));
        }


        return ResponseDTO.buildSuccess(appointmentRecords);
    }

    @Override
    public ResponseDTO<AppointmentRecord> selectAppointmentRecordById(Integer id) throws SysException {

        AppointmentRecord appointmentRecord = appointmentRecordMapper.selectAppointmentRecordById(id);
        if (Objects.isNull(appointmentRecord)){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.QUERY_FAIL));
        }


        return ResponseDTO.buildSuccess(appointmentRecord);
    }

    @Override
    public ResponseDTO addAppoint(AppointmentRecord appointmentRecord) throws SysException {

        /*
            需要注意的时，这里需要编写业务判断
            如果该套餐只适合某种性别，登录用户的性别和套餐不相符。应该时无法预约
            同时对预约时间应该判断。应该对时间做出判断，例如必能预约前一天，也不能预约下午
            同时该时间端，如果有预约，应该不能预约
         */

        int i = appointmentRecordMapper.addAppoint(appointmentRecord);
        if (i<=0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.ADD_ERR));
        }
        return ResponseDTO.buildSuccess();
    }

    @Override
    public ResponseDTO modifyAppoint(AppointmentRecord appointmentRecord) throws SysException {
        int i = appointmentRecordMapper.modifyAppoint(appointmentRecord);
        if (i<=0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.MODIFY_FAIL));
        }
        return ResponseDTO.buildSuccess();
    }

    @Override
    public ResponseDTO removeAppointByID(Integer id) throws SysException {

        int i = appointmentRecordMapper.removeAppointByID(id);
        if (i<=0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.REMOVE_FAIL));
        }
        return ResponseDTO.buildSuccess();
    }

    @Override
    public ResponseDTO confilmAppoint(Integer id) {

        AppointmentRecord appointmentRecord = appointmentRecordMapper.selectAppointmentRecordById(id);

        appointmentRecord.setAppointmentStatus(2);

        appointmentRecordMapper.modifyAppoint(appointmentRecord);

        return ResponseDTO.buildSuccess();
    }
}
